package com.djx.redis.controller;

import com.djx.redis.service.RedisClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author dingjunxiong
 */
@RestController
@RequestMapping("redis/")
public class RedisController {

    @Autowired
    RedisClientService redisClientService;

    @RequestMapping("test")
    public void test() {
        redisClientService.set("name", "张三", 60);
        System.out.println(redisClientService.get("name"));
    }

    /**
     * 保存字符串
     * @param key
     * @param value
     * @param seconds
     * @return
     */
    @PostMapping("set")
    public void set(String key, String value, int seconds) {
        redisClientService.set(key, value, seconds);
    }

    /**
     * 获取字符串
     * @param key
     * @return
     */
    @PostMapping("get")
    public String get(String key) {
        return redisClientService.get(key);
    }

    /**
     * 获取对象
     * @param key
     * @return
     */
    @PostMapping("getObject")
    public Object getObject(String key) {
        return redisClientService.getObject(key);
    }

    /**
     * 保存对象
     * @param key
     * @param object
     * @param seconds
     * @return
     */
    @PostMapping("setObject")
    public void setObject(String key, Object object, int seconds) {
        redisClientService.setObject(key, object, seconds);
    }

    /**
     * 判断是否存在
     * @param key
     * @return
     */
    @PostMapping("exists")
    public Boolean exists(String key) {
        return redisClientService.exists(key);
    }

    /**
     * 设置过期时间
     * @param key
     * @param seconds
     * @return
     */
    @PostMapping("expire")
    public void expire(String key, int seconds) {
        redisClientService.expire(key, seconds);
    }

    /**
     * 删除
     * @param key
     * @return
     */
    @PostMapping("del")
    public void del(String key) {
        redisClientService.del(key);
    }

    /**
     * 剩余生存时间
     * @param key
     * @return
     */
    @PostMapping("ttl")
    public Long ttl(String key) {
        return redisClientService.ttl(key);
    }
}
